package com.glls.common;

import com.glls.common.enums.ResultCodeEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @date 2022/7/16
 * @desc  后端返回给前端的 一个  封装数据的   数据结构
 *
 *
 *
 *
 */
@Data
public class R {

    private Integer code;   // 响应的状态码

    private String message;  // 响应的信息

    private Boolean success;  // 是否成功


    private Map<Object,Object> data = new HashMap<>();  // 封装响应的数据

    private R() {
    }

    // 返回成功的结果
    public static R ok(){
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }



    // 返回失败的结果
    public static R error(){
        R r = new R();
        r.setSuccess(ResultCodeEnum.ERROR.getSuccess());
        r.setCode(ResultCodeEnum.ERROR.getCode());
        r.setMessage(ResultCodeEnum.ERROR.getMessage());
        return r;
    }


    //public static R setResult(Boolean success,String message,Integer code){
    //    R r = new R();
    //    r.setSuccess(success);
    //    r.setCode(code);
    //    r.setMessage(message);
    //
    //    return r;
    //}
    public static R setResult(ResultCodeEnum resultCodeEnum){
        R r = new R();
        r.setSuccess(resultCodeEnum.getSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }




    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }


    public R code(Integer code){
        this.setCode(code);
        return this;
    }



    public R data(Object key,Object value){
        this.data.put(key,value);
        return this;
    }


    public R data(Map<Object,Object> map){
        this.setData(map);
        return this;
    }

}
